/**
 * Created by lesonli on 2016/11/17.
 */
import * as types from '../types'
import api from '../../api/modules/config'
import {
    getStudentList,
    createStudent,
    updateStudentBase,
    deleteStudent
} from '../../api/modules/student'


const state = {
    student_list: [],
    total: 0,
    showLoading: false,
    type_list:[{id:0,name:'意向学员'},{id:1,name:'正式学员'}]
}
var catch_page_list_data = {}
    // getters
    // actions
const actions = {
        get_student_list({
            commit
        }, param) {
            commit(types.STUDENT_SHOW_LOADING)
            // if(param.project_id){
              getStudentList(param).then(res => {
                  if (res.data.res_code === 1) {
                      commit(types.STUDENT_LIST_LOADED,
                          res.data,
                      );
                  }
              })
        },
        add_student({
            commit
        }, params) {
            commit(types.STUDENT_SHOW_LOADING);
            createStudent(params).then(res => {
                if (res.data.res_code === 1) {
                    params._fn();
                    commit(types.STUDENT_ADDED, {
                        data: params,
                        result: res.data.msg
                    });
                }
            })
        },
        update_student({
            commit
        }, params) {
            commit(types.STUDENT_SHOW_LOADING);
            updateStudentBase(params).then(res => {
                if (res.data.res_code === 1) {
                    commit(types.STUDENT_EDITED, params);
                    params._fn();
                }
            })
        }
    }
    // mutations
const mutations = {
    [types.STUDENT_SHOW_LOADING](state) {
        state.showLoading = true;
    },
    [types.STUDENT_LIST_LOADED](state, param) {
        state.total = param.total_count;
        state.student_list = param.msg;
        // state.student_list = param;
        state.showLoading = false;

    },
    [types.SEARCH_STUDENT_LOADING](state) {
        state.isLoading = true;
    },
    [types.STUDENT_ADDED](state, params) {
            state.student_list.push({
                student_id:params.result,
                dealer_id: params.data.dealer_id,
                school: params.data.school,
                written_result: '',
                interview_result: '',
                intent_product_id: params.data.intent_product_id,
                is_regist: params.data.is_regist,
                type: params.data.type,
                phone: params.data.phone,
            })
        state.showLoading = false;
    },
    [types.STUDENT_EDITED](state, params) {
            for (var i = 0; i < state.student_list.length; i++) {
                if (state.student_list[i].student_id === params.student_id) {
                    for(var p in params)
                    {
                        state.student_list[i][p] = params[p];
                    }
                    break;
                }
            }
        state.showLoading = false;
    }
}

export default {
    state,
    actions,
    mutations
}
